#define IN_A 40
#define IN_B 41
#define IN_C 42
#define IN_D 43
#define IN_E 44
#define IN_F 45
#define IN_G 46
#define IN_H 47

#define SEL_A 50
#define SEL_B 51
#define SEL_C 52
#define Z 53 //MUX output

void setup() {
  pinMode(IN_A, OUTPUT);
  pinMode(IN_B, OUTPUT);
  pinMode(IN_C, OUTPUT);
  pinMode(IN_D, OUTPUT);
  pinMode(IN_E, OUTPUT);
  pinMode(IN_F, OUTPUT);
  pinMode(IN_G, OUTPUT);
  pinMode(IN_H, OUTPUT);
  
  pinMode(SEL_A, OUTPUT);
  pinMode(SEL_B, OUTPUT);
  pinMode(SEL_C, OUTPUT);
  pinMode(Z, INPUT);
  Serial.begin(9600);
}

void loop() {
  int i;
  for(i=0; i < 8; i++) {
    digitalWrite(IN_A + i, LOW); //set all mux inputs to low
  }
  for(i=0; i < 8; i++) {
    digitalWrite(IN_A + i, HIGH);
    
    //Set SELECT lines {SEL_C, SEL_B, SEL_A}
    if(i & 1) { digitalWrite(SEL_A, HIGH); }
    else { digitalWrite(SEL_A, LOW); }
    
    if(i & 2) { digitalWrite(SEL_B, HIGH); }
    else { digitalWrite(SEL_B, LOW); }
    
    if(i & 4) { digitalWrite(SEL_C, HIGH); }
    else { digitalWrite(SEL_C, LOW); }
    
    //Test output on a HIGH input
    if(digitalRead(Z) == HIGH) {
      Serial.println("INPUT: " + i);
    }
    else {
      Serial.println("ERROR on INPUT: " + i); 
    }
    
    //Test output on a LOW input
    digitalWrite(IN_A + i, LOW);
    
    if(digitalRead(Z) == LOW) {
      Serial.println("input: " + i); 
    }
    else {
      Serial.println("ERROR on input: " + i);
    }
  }
  
  delay(60000);
}
